package chainOfResponsibility;

import gameBasics.GameModel;
import gameBasics.GameObject;
import gameBasics.PropertyManager;
import gameLevel.GameLevel;
import gameLevel.GameLevelControl;
import tank.Tank;
import tank.TankType;
import tank.bullet.Bullet;
import tank.bullet.BulletGroup;
import tank.explosion.Explosion;

import java.util.Objects;

public class BulletTankCollider implements Collider {

    private static int enemyTankNumInCL1 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL1"));
    private static int enemyTankNumInCL2 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL2"));
    private static int enemyTankNumInCL3 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL3"));
    private static int enemyTankNumInCL4 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL4"));
    private static int enemyTankNumInSL = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfSL"));

    /**
     * 炮弹与坦克碰撞
     * @param o1 炮弹对象
     * @param o2 坦克对象
     */
    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if (o1 instanceof Bullet bullet && o2 instanceof Tank tank) {
            return !collideWith(bullet, tank);
        } else if (o1 instanceof Tank && o2 instanceof Bullet) {
            return !collide(o2, o1);
        }

        return true;
    }

    /**
     * 炮弹与坦克的碰撞检测
     * @param bullet 炮弹对象
     * @param tank 坦克对象
     */
    public boolean collideWith(Bullet bullet, Tank tank) {
        if((bullet.getBulletGroup() == BulletGroup.GOOD && tank.getTankType() != TankType.MAIN) || (bullet.getBulletGroup() == BulletGroup.BAD && tank.getTankType() == TankType.MAIN)){
            //若炮弹与坦克相交则认为炮弹击杀了坦克
            if(bullet.getRectangle().intersects(tank.getRectangle())){
                tank.die();     //坦克消亡
                bullet.die();   //炮弹消亡
                new Explosion(tank.getX() + Tank.WIDTH / 2 - Explosion.WIDTH / 2,tank.getY() + Tank.HEIGHT / 2 - Explosion.HEIGHT / 2);
                //处理碰撞结果
                collideResult();

                return true;    //相撞
            }
        }

        return false;
    }

    /**
     * 炮弹与坦克的碰撞结果
     * 用于判断关卡是否结束以及关卡的切换
     */
    private void collideResult() {
        //主坦克被炮弹击中, 游戏结束
        if(!GameModel.getInstance().getMainTank().isLivingState()) GameLevelControl.gameOverState = true;

        //游戏未结束, 结合所处关卡进行判断
        if(!GameLevelControl.gameOverState)
        {
            switch (GameLevelControl.gameLevel)
            {
                case CLASSIC_LEVE1 ->
                {
                    enemyTankNumInCL1--;
                    GameLevelControl.passState = enemyTankNumInCL1 == 0;
                    if(GameLevelControl.passState) GameLevelControl.nowClassicGameLevel = GameLevel.CLASSIC_LEVE2;
                }
                case CLASSIC_LEVE2 ->
                {
                    enemyTankNumInCL2--;
                    GameLevelControl.passState = enemyTankNumInCL2 == 0;
                    if(GameLevelControl.passState) GameLevelControl.nowClassicGameLevel = GameLevel.CLASSIC_LEVE3;
                }
                case CLASSIC_LEVE3 ->
                {
                    enemyTankNumInCL3--;
                    GameLevelControl.passState = enemyTankNumInCL3 == 0;
                    if(GameLevelControl.passState) GameLevelControl.nowClassicGameLevel = GameLevel.CLASSIC_LEVE4;
                }
                case CLASSIC_LEVE4 ->
                {
                    enemyTankNumInCL4--;
                    GameLevelControl.passState = enemyTankNumInCL4 == 0;
                    if(GameLevelControl.passState) GameLevelControl.gameOverState = true;
                }
                case SURVIVAL ->
                {
                    enemyTankNumInSL--;
                    GameLevelControl.passState = enemyTankNumInSL == 0;
                    if(GameLevelControl.passState) GameLevelControl.gameOverState = true;
                }
            }
        }
    }

    /**
     * 重置经典关卡中坦克的数量
     */
    public static void resetTheNumOfEnemyTank(){
        enemyTankNumInCL1 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL1"));
        enemyTankNumInCL2 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL2"));
        enemyTankNumInCL3 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL3"));
        enemyTankNumInCL4 = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfCL4"));
        enemyTankNumInSL = Objects.requireNonNull(PropertyManager.getInt("enemyTankNumOfSL"));
    }

}
